/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence.brokersDB;

import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import systole.domain.persons.profession.Profession;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ProfessionSyncBroker;
import systole.synchronization.remote.entities.ProfessionRemote;

/**
 *
 * @author Juan
 */
public class ProfessionSyncBrokerDB extends BrokerDB implements ProfessionSyncBroker {

    /**
     * 
     */
    public ProfessionSyncBrokerDB() {
        super();
    }

    public void saveProfessionRemote(ProfessionRemote professionRemote) throws ExceptionDAO {
           try {
            this.logger.logDebug("saving remote profession");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(professionRemote);
            this.logger.logDebug("remote profession saved");
        } catch (HibernateException e) {
            this.logger.logError("error on save profession remote, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron guardar los cambios", e.fillInStackTrace());
        }
    }

    public List<ProfessionRemote> getProfessionsToUpdate() throws ExceptionDAO {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public List<Profession> getProfessionsToUpload() throws ExceptionDAO {
        try {
            this.logger.logDebug("getting professions to upload");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            String hql = "select p from ProfessionRemote pr right join pr.profession p where pr=null";
            Query query = currentSession.createQuery(hql);
            @SuppressWarnings("unchecked")
            List<Profession> list = query.list();
            this.logger.logDebug("get successfully");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get professions to upload, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las profesiones", e.fillInStackTrace());
        }
    }

    public Profession getProfessionByRemoteId(int remoteId) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting profession by remote id");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    ProfessionRemote.class).add(Restrictions.eq("remoteId", remoteId)).setMaxResults(1);
            ProfessionRemote professionsRemote = (ProfessionRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (professionsRemote != null ? professionsRemote.getProfession() : null);
        } catch (HibernateException e) {
            this.logger.logError("error on get profession by remote id, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las profesiones", e.fillInStackTrace());
        }
    }

    public ProfessionRemote getProfessionRemoteByProfession(Profession profession) throws ExceptionDAO {
        try {
            this.logger.logDebug("getting profession remote by local profession");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    ProfessionRemote.class).add(Restrictions.eq("profession", profession)).setMaxResults(1);
            ProfessionRemote medicineRemote = (ProfessionRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return medicineRemote;
        } catch (HibernateException e) {
            this.logger.logError("error on get remote profession by profession, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las profesiones", e.fillInStackTrace());
        }
    }

    public int getLastRemoteProfessionIdSynchronized() throws ExceptionDAO {
         try {
            this.logger.logDebug("getting last remote profession sync");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            Criteria criteria = currentSession.createCriteria(
                    ProfessionRemote.class).addOrder(Order.desc("remoteId")).setMaxResults(1);
            ProfessionRemote professionRemote = (ProfessionRemote) criteria.uniqueResult();
            this.logger.logDebug("get successfully");
            return (professionRemote != null ? professionRemote.getRemoteId() : -1);
        } catch (HibernateException e) {
            this.logger.logError("error on get last remote profession synchronized, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudieron obtener las profesiones", e.fillInStackTrace());
        }
    }




}
